/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package ga.tsp;

import java.util.ArrayList;
import java.util.List;

import org.jheuristics.Individual;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.operators.Probability;
import org.jheuristics.ga.operators.reproductors.AbstractXOver;

public class GreedyXOver extends AbstractXOver {
	private RouteEvaluator routeEvaluator;

	/**
	 * TODO
	 *
	 * @param probability
	 * @param routeEvaluator
	 */
	public GreedyXOver(Probability probability, RouteEvaluator routeEvaluator) {
		super(probability, 0);
		setRouteEvaluator(routeEvaluator);
	}

	/**
	 * TODO
	 *
	 * @return
	 */
	public RouteEvaluator getRouteEvaluator() {
		return routeEvaluator;
	}

	/**
	 * TODO
	 *
	 * @param routeEvaluator
	 */
	public void setRouteEvaluator(RouteEvaluator routeEvaluator) {
		if (null == routeEvaluator) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		this.routeEvaluator = routeEvaluator;
	}

	public Individual[] reproduce(Individual[] parents, GAStatus status, GAConfig config) {
		Individual[] result = super.reproduce(parents, status, config);
		if (result.length != 1) {
			return result[0].compareTo(result[1]) > 0 ? new Individual[] {result[0]} : new Individual[] {result[1]}; 
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.operators.reproductors.AbstractXOver#xover(org.jheuristics.ga.Individual[], org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	protected Individual[] xover(Individual[] parents, GAStatus status, GAConfig config) {
	    if (!parents[0].equals(parents[1])) {
	        return doXOver(parents[0], parents[1], status, config);
	    }
	    return new Individual[] {parents[0], parents[1]};
	}

	/**
	 * TODO
	 *
	 * @param parent1
	 * @param parent2
	 * @param status
	 * @param config
	 * @return
	 */
	protected Individual[] doXOver(Individual parent1, Individual parent2, GAStatus status, GAConfig config) {
		List gensParent1 = (List) parent1.getGens();
		List gensParent2 = (List) parent2.getGens();
		List avaliablePositions = new ArrayList(gensParent1);
		List gens = new ArrayList(gensParent1.size());

		Object currentPosition = routeEvaluator.getInitialPosition();
		int indexCurrentPosition = 0;
		while (indexCurrentPosition < gensParent1.size() - 1) {
			List avaliableRoutes = new ArrayList();
			avaliableRoutes.add(findNextPoition(gensParent1, currentPosition));
			avaliableRoutes.add(findNextPoition(gensParent2, currentPosition));

			Object position = routeEvaluator.getBestRoute(currentPosition, avaliableRoutes.toArray());

			if (gens.contains(position)) {
				avaliableRoutes.remove(position);
				position = avaliableRoutes.get(0);
				if (gens.contains(position)) {
					position = avaliablePositions.get(0);
				}
			}
			avaliablePositions.remove(position);
			gens.add(indexCurrentPosition++, position);
			currentPosition = position;
		}
		gens.add(avaliablePositions.get(0));

		return new Individual[] {config.getIndividualsFactory().createSpecificIndividual(gens, config)};
	}

	/**
	 * TODO
	 *
	 * @param gens
	 * @param current
	 * @return
	 */
	private Object findNextPoition(List gens, Object current) {
		int index = gens.indexOf(current);
		if (gens.size()-1 == index || -1 == index) {
			return gens.get(0);
		} else {
			return gens.get(index+1);
		}
	}

}
